package com.ecarx.vhaltests.hy11.testcases.climate;

import android.car.VehicleAreaSeat;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import com.ecarx.vhaltests.Utils.CarPropertyEventTester;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.vhaltests.hy11.testcases.Utils.IpcpClientHelper;
import com.ecarx.xui.adaptapi.car.Car;
import com.ecarx.xui.adaptapi.car.IWrapper;
import com.ecarx.xui.adaptapi.car.hvac.IHvac;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HvacFuncAutoBlowingModeTest extends VhalUnitTestBase {
    private static final String TAG = "HVAC_FUNC_AUTO_BLOWING_MODE_TEST";
    private static int sPropId;

    private static int sFace;
    private static int sLeg;
    private static int sFaceLeg;
    private static int sFrontWindow;
    private static int sFaceFrontWindow;
    private static int sLegFrontWindow;
    private static int sAll;
    private static int sAuto;

    private static final int FLR = 0;
    private static final int VENT = 1;
    private static final int DEFRST = 2;
    private static final int FLR_DEFRST = 3;
    private static final int FLR_VENT = 4;
    private static final int VENT_DEFRST = 5;
    private static final int FLR_VENT_DEFRST = 6;
    private static final int AUT = 7;

    private final static IpcpClientHelper sClient = new IpcpClientHelper();
    private static CarPropertyEventTester sPropertyEvent;

    private static final String PA_CL_MODE_FRST_LEFT = "paclmodefrstleft";
    private static final String PA_CL_MODE_SEC = "paclmodesec";

    private static final int AVAIL_ACTIVE = 1;
    private static final int AVAIL_NOT_ACTIVE = 2;
    private static final int AVAIL_NOT_AVAILABLE = 3;
    private static final int AVAIL_ERROR = 3;

    private static final int ROW_1 = VehicleAreaSeat.SEAT_ROW_1_LEFT | VehicleAreaSeat.SEAT_ROW_1_RIGHT;
    private static final int ROW_2 = VehicleAreaSeat.SEAT_ROW_2_LEFT | VehicleAreaSeat.SEAT_ROW_2_RIGHT;

    @BeforeClass
    public static void initTest() {
        IWrapper wrapper = Car.createWrapper(mContext);
        IWrapper.IPropertyId propid = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, IHvac.HVAC_FUNC_AUTO_BLOWING_MODE);
        sPropId = propid.getPropertyId();
        sPropertyEvent = new CarPropertyEventTester(sPropId, value -> {
            Log.d(TAG, "value=[" + value + "]");
        });
        mCarPropertyMgr.registerCallback(sPropertyEvent, sPropId, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        Log.d(TAG, "sPropId=" + sPropId);
        Log.d(TAG, "ROW_1=[" + ROW_1 + "], ROW_2=[" + ROW_2 + "]");
        initArgs(propid);
    }

    private static void makeActive() {
        sendPA_CL_Mode(AVAIL_ACTIVE, FLR);
        delay(3000);
    }

    private static void initArgs(IWrapper.IPropertyId propid) {
        sFace = propid.getPropertyValue(IHvac.BLOWING_MODE_FACE);
        sLeg = propid.getPropertyValue(IHvac.BLOWING_MODE_LEG);
        sFaceLeg = propid.getPropertyValue(IHvac.BLOWING_MODE_FACE_AND_LEG);
        sFrontWindow = propid.getPropertyValue(IHvac.BLOWING_MODE_FRONT_WINDOW);
        sFaceFrontWindow = propid.getPropertyValue(IHvac.BLOWING_MODE_FACE_AND_FRONT_WINDOW);
        sLegFrontWindow = propid.getPropertyValue(IHvac.BLOWING_MODE_LEG_AND_FRONT_WINDOW);
        sAll = propid.getPropertyValue(IHvac.BLOWING_ALL);
        sAuto = propid.getPropertyValue(IHvac.BLOWING_MODE_AUTO_SWITCH);
        Log.d(TAG, "IHvac.BLOWING_MODE_FACE=[" + IHvac.BLOWING_MODE_FACE + "]," + " IHvac.BLOWING_MODE_LEG=[" + IHvac.BLOWING_MODE_LEG + "]," + " IHvac.BLOWING_MODE_FACE_AND_LEG=[" + IHvac.BLOWING_MODE_FACE_AND_LEG + "]," + " IHvac.BLOWING_MODE_FRONT_WINDOW=[" + IHvac.BLOWING_MODE_FRONT_WINDOW + "]," + " IHvac.BLOWING_MODE_FACE_AND_FRONT_WINDOW=[" + IHvac.BLOWING_MODE_FACE_AND_FRONT_WINDOW + "]," + " IHvac.BLOWING_MODE_LEG_AND_FRONT_WINDOW=[" + IHvac.BLOWING_MODE_LEG_AND_FRONT_WINDOW + "]," + " IHvac.BLOWING_ALL=[" + IHvac.BLOWING_ALL + "]," + " IHvac.BLOWING_MODE_AUTO_SWITCH=[" + IHvac.BLOWING_MODE_AUTO_SWITCH + "]");
        Log.d(TAG, "sFace=[" + sFace + "]," + " sLeg=[" + sLeg + "]," + " sFaceLeg=[" + sFaceLeg + "]," + " sFrontWindow=[" + sFrontWindow + "]," + " sFaceFrontWindow=[" + sFaceFrontWindow + "]," + " sLegFrontWindow=[" + sLegFrontWindow + "]," + " sAll=[" + sAll + "]," + " sAuto=[" + sAuto + "]");
    }

    private static int convertValue(int value) {
        switch (value) {
            case FLR:
                return sLeg;
            case VENT:
                return sFace;
            case DEFRST:
                return sFrontWindow;
            case FLR_DEFRST:
                return sLegFrontWindow;
            case FLR_VENT:
                return sFaceLeg;
            case VENT_DEFRST:
                return sFaceFrontWindow;
            case FLR_VENT_DEFRST:
                return sAll;
            case AUT:
            default:
                return sAuto;
        }
    }

    private static void sendPA_CL_ModeFrstLeft(int avail, int data) {
        sClient.setClimateValues(new String[]{PA_CL_MODE_FRST_LEFT + ".availability", PA_CL_MODE_FRST_LEFT + ".data"}, new int[]{avail, data});
    }

    private static void sendPA_CL_ModeSec(int avail, int data) {
        sClient.setClimateValues(new String[]{PA_CL_MODE_SEC + ".availability", PA_CL_MODE_SEC + ".data"}, new int[]{avail, data});
    }

    private static void sendPA_CL_Mode(int avail, int data) {
        sClient.setClimateValues(new String[]{PA_CL_MODE_FRST_LEFT + ".availability", PA_CL_MODE_FRST_LEFT + ".data", PA_CL_MODE_SEC + ".availability", PA_CL_MODE_SEC + ".data"}, new int[]{avail, data, avail, data});
    }

    @Test
    public void test001_hvac_func_auto_blowing_mode_not_available() {
        Log.d(TAG, "**************************AVAIL_NOT_AVAILABLE>");
        sendPA_CL_Mode(AVAIL_NOT_AVAILABLE, AUT);
        delay(1000);
        boolean ret = mCarPropertyMgr.isPropertyAvailable(sPropId, ROW_1);
        Assert.assertFalse(ret);
        ret = mCarPropertyMgr.isPropertyAvailable(sPropId, ROW_2);
        Assert.assertFalse(ret);
        Log.d(TAG, "**************************AVAIL_NOT_AVAILABLE<");
    }

    @Test
    public void test002_hvac_func_auto_blowing_mode_not_active() {
        Log.d(TAG, "**************************AVAIL_NOT_ACTIVE>");
        sendPA_CL_ModeFrstLeft(AVAIL_NOT_ACTIVE, AUT);
        delay(1000);
        Assert.assertEquals(CarPropertyEventTester.STS_NotActive, sPropertyEvent.getLastStatus());
        sendPA_CL_ModeSec(AVAIL_NOT_ACTIVE, AUT);
        delay(1000);
        Assert.assertEquals(CarPropertyEventTester.STS_NotActive, sPropertyEvent.getLastStatus());
        Log.d(TAG, "**************************AVAIL_NOT_ACTIVE<");
    }

    private void testValue(int v) {
        int value = convertValue(v);
        mCarPropertyMgr.setIntProperty(sPropId, ROW_1, value);
        mCarPropertyMgr.setIntProperty(sPropId, ROW_2, value);
        delay();
        sendPA_CL_Mode(AVAIL_ACTIVE, v);
        delay(1000);
        int ret = mCarPropertyMgr.getIntProperty(sPropId, ROW_1);
        Assert.assertEquals(value, ret);
        ret = mCarPropertyMgr.getIntProperty(sPropId, ROW_2);
        Assert.assertEquals(value, ret);
    }

    @Test
    public void test003_hvac_func_auto_blowing_mode_set_value() {
        makeActive();
        Log.d(TAG, "**************************FLR>");
        testValue(FLR);
        Log.d(TAG, "**************************FLR<");
        Log.d(TAG, "**************************VENT>");
        testValue(VENT);
        Log.d(TAG, "**************************VENT<");
        Log.d(TAG, "**************************DEFRST>");
        testValue(DEFRST);
        Log.d(TAG, "**************************DEFRST<");
        Log.d(TAG, "**************************AUT>");
        testValue(AUT);
        Log.d(TAG, "**************************AUT<");
    }
}
